<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="订单号" prop="order_id">
                    <el-input v-model="formData.order_id"  readonly placeholder="请输入订单号" />
                </el-form-item>
                <el-form-item label="商品名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入商品名称" />
                </el-form-item>
                <el-form-item label="产品类型" prop="unit">
                    <el-select class="flex-1" v-model="formData.protype" clearable placeholder="请选择产品类型">
                        <el-option 
                            v-for="(item, index) in dictData.protype"
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="规格" prop="guige">
                    <el-input v-model="formData.guige" clearable placeholder="请输入规格" />
                </el-form-item>
                <el-form-item label="数量" prop="num">
                    <el-input v-model="formData.num" clearable placeholder="请输入数量" />
                </el-form-item>
                <el-form-item label="单位" prop="unit">
                    <el-select class="flex-1" v-model="formData.unit" clearable placeholder="请选择单位">
                        <el-option 
                            v-for="(item, index) in dictData.unit"
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="单价" prop="price">
                    <el-input v-model="formData.price" clearable placeholder="请输入单价" />
                </el-form-item>
                <el-form-item label="物料编号" prop="wuliaoNo">
                    <el-input v-model="formData.wuliaoNo" clearable placeholder="请输入物料编号" />
                </el-form-item>
                <el-form-item label="图纸" prop="tuzhi">
                    <material-picker v-model="formData.tuzhi" />
                </el-form-item>
                
                
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="客户订单号" prop="orderNo">
                    <el-input v-model="formData.orderNo" clearable placeholder="请输入客户订单号" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="ordersDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiOrdersDetailAdd, apiOrdersDetailEdit, apiOrdersDetailDetail } from '@/api/orders/orders_detail'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑订单详情' : '新增订单详情'
})

// 表单数据
const formData = reactive({
    id: '',
    order_id: '',
    name: '',
    guige: '',
    num: '',
    unit: '',
    price: '',
    wuliaoNo: '',
    tuzhi: '',
    protype:'',
    remark: '',
    orderNo: '',
})


// 表单验证
const formRules = reactive<any>({

})


// 获取详情
const setFormData = async (row: Record<any, any>) => {
    let data:any = {}
  
    if(!row.id){
        data['order_id']=row.order_id
    }else{
        data = await apiOrdersDetailDetail({ id: row.id })
    }

    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
   
    
    
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData,  }
    mode.value == 'edit' 
        ? await apiOrdersDetailEdit(data) 
        : await apiOrdersDetailAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()

}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData
})
</script>
